﻿Imports System.ServiceModel
Imports System.ComponentModel

Public Class MainForm
    Public Sub New()
        Me.InitializeComponent()
        Me.InitializeServiceHost()
        Me.InitializeContextMenu()
    End Sub

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If (disposing AndAlso (Not Me.components Is Nothing)) Then
            Me.components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private Sub exitMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
        If (Me.host.State <> CommunicationState.Closed) Then
            Me.host.Close()
        End If
        MyBase.Close()
    End Sub

    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        ' Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(MainForm))
        Me.serviceTrayIcon = New System.Windows.Forms.NotifyIcon(Me.components)
        Me.SuspendLayout()
        '
        'serviceTrayIcon
        '
        Me.serviceTrayIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info
        Me.serviceTrayIcon.Icon = My.Resources.serviceunknown
        Me.serviceTrayIcon.Visible = True
        '
        'MainForm
        '
        Me.ClientSize = New System.Drawing.Size(284, 264)
        Me.Name = "MainForm"
        Me.ShowInTaskbar = False
        Me.Text = "Service"
        Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
        Me.ResumeLayout(False)

    End Sub

    Private Sub InitializeContextMenu()
        Dim list As New List(Of MenuItem)
        Me.startMenu = New MenuItem("Start service")
        AddHandler Me.startMenu.Click, New EventHandler(AddressOf Me.startMenu_Click)
        Me.stopMenu = New MenuItem("Stop service")
        AddHandler Me.stopMenu.Click, New EventHandler(AddressOf Me.stopMenu_Click)
        Dim exitMenu As New MenuItem("E&xit")
        AddHandler exitMenu.Click, New EventHandler(AddressOf Me.exitMenu_Click)
        list.Add(Me.startMenu)
        list.Add(Me.stopMenu)
        list.Add(New MenuItem("-"))
        list.Add(exitMenu)
        Me.serviceTrayIcon.ContextMenu = New ContextMenu(list.ToArray)
    End Sub

    Private Sub InitializeServiceHost()
        '服务的类
        Me.host = New ServiceHost(GetType(SonOVB.Samples.ComicSyndication.ComicService))
        AddHandler Me.host.Opened, New EventHandler(AddressOf Me.OnServiceHostOpened)
        AddHandler Me.host.Opening, New EventHandler(AddressOf Me.OnServiceHostOpening)
        AddHandler Me.host.Closed, New EventHandler(AddressOf Me.OnServiceHostClosed)
    End Sub

    Private Sub MainForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Me.host.Open()
    End Sub

    Private Sub OnServiceHostClosed(ByVal sender As Object, ByVal e As EventArgs)
        Me.startMenu.Enabled = True
        Me.stopMenu.Enabled = False
        Me.serviceTrayIcon.Text = "Service is stopped"
        Me.serviceTrayIcon.ShowBalloonTip(3, "WinHost", Me.serviceTrayIcon.Text, ToolTipIcon.Info)
        Me.serviceTrayIcon.Icon = My.Resources.servicestopped
    End Sub

    Private Sub OnServiceHostOpened(ByVal sender As Object, ByVal e As EventArgs)
        Me.startMenu.Enabled = False
        Me.stopMenu.Enabled = True
        Me.serviceTrayIcon.Text = "Service is running"
        Me.serviceTrayIcon.ShowBalloonTip(3, "WinHost", Me.serviceTrayIcon.Text, ToolTipIcon.Info)
        Me.serviceTrayIcon.Icon = My.Resources.servicerunning

    End Sub

    Private Sub OnServiceHostOpening(ByVal sender As Object, ByVal e As EventArgs)
        Me.startMenu.Enabled = False
        Me.stopMenu.Enabled = False
        Me.serviceTrayIcon.Text = "Service is opening"
        Me.serviceTrayIcon.ShowBalloonTip(3, "WinHost", Me.serviceTrayIcon.Text, ToolTipIcon.Info)
        Me.serviceTrayIcon.Icon = My.Resources.serviceunknown
    End Sub

    Private Sub startMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Me.InitializeServiceHost()
            Me.host.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub stopMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Me.host.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    ' Fields
    Private components As IContainer = Nothing
    Private host As ServiceHost = Nothing
    Private serviceTrayIcon As NotifyIcon
    Private startMenu As MenuItem = Nothing
    Private stopMenu As MenuItem = Nothing
End Class
